package com.mvvm.ktnet


import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BaseObservable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.rxLifeScope
import com.alibaba.fastjson.JSON
import com.android.ktx.coroutinescope.Ktx
import com.android.ktx.view.onClick
import com.drake.logcat.LogCat

import com.drake.tooltip.toast
import com.google.gson.Gson
import com.gyf.immersionbar.ktx.immersionBar
import com.mvvm.ktnet.application.TOAST_ERROR
import com.mvvm.ktnet.base.activity.IBaseVmActivity
import com.mvvm.ktnet.base.myappbase.BaseActivity
import com.mvvm.ktnet.base.myappbase.BaseDbActivity
import com.mvvm.ktnet.databinding.ActivityMainBinding
import com.mvvm.ktnet.entity.TestDao
import com.mvvm.ktnet.ext.LogJsonString
import com.mvvm.ktnet.ext.loadmore
import com.mvvm.ktnet.ext.refresh
import com.mvvm.ktnet.http.base.UrlPath
import com.mvvm.ktnet.intent.openActivity
import com.mvvm.ktnet.retention.ViewModelActivity
import com.mvvm.ktnet.retention.ViewModelApp
import com.mvvm.ktnet.ui.amap.AmapActivity
import com.mvvm.ktnet.ui.dao.TestDaoActivity
import com.mvvm.ktnet.ui.drag.TestDrag
import com.mvvm.ktnet.ui.keyEvent.EventAc
import com.mvvm.ktnet.ui.recyclerView.TestRecyclerview
import com.mvvm.ktnet.ui.rili.RiLiAc
import com.mvvm.ktnet.ui.testChart.TestChart
import com.mvvm.ktnet.ui.testScrollView.TestScroll
import com.mvvm.ktnet.ui.testViewPager2.TestViewPager2
import com.mvvm.ktnet.ui.viewmodel.MainViewModel
import com.mvvm.ktnet.utils.CopyDao
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import nethttp.RxHttp
import nethttp.toResponse
import nethttp.toResponseData
import rxhttp.async
import rxhttp.toStr
import java.util.*
import kotlin.coroutines.resume
import kotlin.random.Random

class MainActivity : BaseDbActivity<MainViewModel, ActivityMainBinding>() {


    @ViewModelActivity
    var mainViewModel: MainViewModel? = null

    @ViewModelApp
    var mainViewModelApp: MainViewModel? = null

    override fun layoutId() = R.layout.activity_main


    lateinit var bluetoothAdapter: BluetoothAdapter

    suspend fun test() = suspendCancellableCoroutine<String> {


        it.resume("first")


    }

    override fun createObserver() {

//        mViewModel.liveString.observe(this, {
//            LogCat.e("这是自带的")
//            toast(JSON.toJSONString(it))
//        })
//
//        mainViewModel?.liveString?.observe(this, {
//
//            LogCat.e("这是后面新建的")
//            toast("测试ac注解 ${JSON.toJSONString(it)}")
//
//        })
//
//
//        mainViewModelApp?.liveString?.observe(this, {
//
//            LogCat.e("这是app的")
//            toast("测试app注解 ${JSON.toJSONString(it)}")
//
//        })
    }

    override fun immersionBarEnabled(): Boolean = true
    override fun initStatusBar() {
        immersionBar {
            statusBarView(status)
            autoDarkModeEnable(true)
            statusBarColor(R.color.color22BD82)
        }
    }

    override fun initData() {
//        mainViewModel?.getTestString()
//        mainViewModelApp?.getTestString()
    }

    val userDataLive = MutableLiveData<UserDataBinding>()

    var exitTime = 0L
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.mainmodel = mViewModel

        mDatabind.useData = userDataLive.value
        mDatabind.mainActivity = this

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

        LogCat.json(Gson().toJson(Locale.getDefault()), "测试的")
        LogCat.e(Locale.getDefault().displayCountry, "测试的")
        LogCat.e(Locale.getDefault().country, "测试的")
        LogCat.e(Locale.getDefault().language, "测试的")

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    toast("再按一次退出程序")
                    exitTime = System.currentTimeMillis()
                } else {
                    finish()
                }
            }
        })

        mDatabind.openBle.onClick {

            if (bluetoothAdapter.enable()) {
                toast("权限允许")
            } else toast("权限不允许")

        }

//        smartRefreshLayout?.loadmore {
//
//
//        }?.refresh {
//
//
//        }
//
//        toast_default.setOnClickListener {
//            mViewModel.getTestString()
//        }
//
//        toast_error.setOnClickListener {
//
//        }

        mDatabind.toChart.onClick {

            val intent = Intent(this, TestChart::class.java)
            startActivity(intent)
        }


        mDatabind.toScroll.onClick {

            val intent = Intent(this, TestScroll::class.java)
            startActivity(intent)
        }
        mDatabind.toViewPager2.onClick {

            val intent = Intent(this, TestViewPager2::class.java)
            startActivity(intent)
        }
        mDatabind.toRecycler.onClick {

            val intent = Intent(this, TestRecyclerview::class.java)
            startActivity(intent)
        }
        mDatabind.dragDown.onClick {

            val intent = Intent(this, TestDrag::class.java)
            startActivity(intent)
        }
        mDatabind.smallBig.onClick {


            val intent = Intent(this, TestDaoActivity::class.java)
            startActivity(intent)
        }
        mDatabind.useBinding.onClick {

            userDataLive.value = UserDataBinding(Random.nextInt(1000).toString())

        }

        mDatabind.map.onClick {
            openActivity<AmapActivity>()
        }
        mDatabind.keyEvent.onClick {
            openActivity<EventAc>()
        }
        mDatabind.rili.onClick {
            openActivity<RiLiAc>()
        }
        rxLifeScope.launch({
            Ktx.runIo {
                CopyDao.copyDbFile(this@MainActivity, "student.sqlite")
            }.await()
        }, {
            Log.e("copy", "initView: " + it.message)
            it.printStackTrace()
        })


    }

    data class UserDataBinding(var use: String)

    override fun reLoad() {
//        toast("重试")
//        mViewModel.getTestString()
    }
}
